<template>
  <div class="section_common">
    <div class="main">
      <el-form ref="form" label-width="100px">

        <el-form-item label="内推人员">
          <el-input v-model="ntUser" size="mini" readonly></el-input>
        </el-form-item>

        <el-form-item label="内推部门" required><!--multiple-->
          <el-select v-model="deptName" :filterable="true" size="mini" placeholder="请选择" @change="selectDept">
            <el-option v-for="(item,index) in deptField" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="部门" required>
          <el-input v-model="deptName" size="mini" @focus="showDept = true" readonly></el-input>
          <van-popup v-model="showDept" position="bottom">
            <van-picker
                    show-toolbar
                    :columns="deptField"
                    @confirm="selectDept"
                    @cancel="showDept = false"
            />
          </van-popup>
        </el-form-item>-->

        <el-form-item label="内推岗位" required><!--multiple-->
          <el-select v-model="posName" :filterable="true" size="mini" placeholder="请选择" @change="selectPos">
            <el-option v-for="(item,index) in postField" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="指定内推组别">
          <el-radio-group v-model="zdGroup">
            <el-radio  label="是">是</el-radio>
            <el-radio  label="否">否</el-radio>
          </el-radio-group>

        </el-form-item>
        <el-form-item label="组别名称" required v-if="this.zdGroup=='是'">
          <el-input v-model="groupName" size="mini"></el-input>
        </el-form-item>
        <!--<el-form-item label="岗位" required>
          <el-input v-model="posName" size="mini" @focus="showPos = true" readonly></el-input>
          <van-popup v-model="showPos" position="bottom">
            <van-picker
                    show-toolbar
                    :columns="postField"
                    @confirm="selectPos"
                    @cancel="showPos = false"
            />
          </van-popup>
        </el-form-item>-->

        <el-form-item label="英语等级" required v-if="!this.showEnglishOther">
          <el-select v-model="english" filterable @change="changeEnglish" default-first-option placeholder="请选择" size="mini">
            <el-option
                    v-for="item in englishs"
                    :key="item"
                    :label="item"
                    :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="英语等级" required v-if="this.showEnglishOther">
          <el-input v-model="english" size="mini"></el-input>
          <!--<el-tag size="mini" style="margin-left: 10px">请对其英语等级进行说明</el-tag>-->
        </el-form-item>

        <el-form-item label="推送HR" v-show="isShowHR"><!--multiple-->
          <el-select v-model="designatedPusher" :filterable="true" size="mini" placeholder="请选择" @change="selectPusher">
            <el-option v-for="(item,index) in pusherField" :key="index" :label="item.label" :value="item.value" :selected="item.select"></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="推送HR" required>
          <el-input v-model="designatedPusher" size="mini" @focus="showPusher = true" readonly></el-input>
          <van-popup v-model="showPusher" position="bottom">
            <van-picker
                    show-toolbar
                    :columns="pusherField"
                    @confirm="selectPusher"
                    @cancel="showPusher = false"
            />
          </van-popup>
        </el-form-item>-->

        <el-form-item label="被推荐人姓名">
          <el-input v-model="uname" size="mini"></el-input>
        </el-form-item>


        <el-form-item label="性别" required>
          <el-radio v-model="usex" label="M">男</el-radio>
          <el-radio v-model="usex" label="F">女</el-radio>
        </el-form-item>

        <el-form-item label="联系方式" required>
          <el-input v-model="uphone" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="关系" required>
          <el-input v-model="relation" size="mini"></el-input>
        </el-form-item>

        <el-form-item label="附件简历" required>
          <input id="file" type="file" ref="inputer" multiple  @change="afterRead()"/><!--accept="application/pdf"-->
        </el-form-item>


        <el-form-item>
          <el-button v-bind:disabled="isShow" type="primary" size="mini" @click="submitPost"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import Vue from 'vue';
  import {Toast, Calendar,Form ,Picker,Popup} from "vant";
  Vue.use(Calendar);
  Vue.use(Form);
  Vue.use(Picker);
  Vue.use(Popup);
export default {
  data() {
    return {
      isShowHR:true,
      designatedPusher: "",//指定推送人
      deptId:"",
      deptName:"",//部门名称
      posId:"",//岗位名称
      posName:"",
      uname:"",
      usex:"M",
      uphone:"",
      relation:"",
      nowUser: '',
      title: "内推申请",
      columns: [],
      fileList: [],
      fileName:"",
      fileUrl:"",
      numPages:"",
      addPush: '/mobiles/addPush',
      uploadImageUrl: '/mobiles/uploadFile',
      treeShow: false,
      currDept: {},
      treeData: [],
      postField:[],
      deptField:[],
      pusherField:[],
      posList:[],
      deptList:[],
      showPos:false,
      showDept:false,
      pusherList:[],
      showPusher:false,
      isShow:false,
      message:"确认内推",
      did:"",
      pid:"",
      ntUser:"",
      zdGroup:"否",
      groupName:"",
      english:'',
      showEnglishOther:false,
      englishs:['无','四级','六级','专四','专八','雅思4.5以上','托福60分以上','其他'],
    };
  },
  created() {
    this.nowUser=JSON.parse(localStorage.getItem('userInfo')).userId;
    var jobNumId = JSON.parse(localStorage.getItem('userInfo')).jobNumId;
    this.ntUser=jobNumId + "/" +JSON.parse(localStorage.getItem('userInfo')).name;
    if (jobNumId.indexOf("WG")!=-1 && jobNumId!="WG2032"){//包含深圳，是深圳用户，需要隐藏招聘hr
      this.isShowHR = false
    }

    this.selDept();
    this.getDesignatedPusher();
    debugger
    this.deptName= this.$route.params.deptName;
    this.posName= this.$route.params.posName;
    this.did= this.$route.params.did;
    this.pid= this.$route.params.pid;
  },

  methods: {

    changeEnglish(value){
      if (value=='其他'){
        this.english='';
        this.showEnglishOther=true;
      }
    },

    //格式化物品归宿地
    formatLacation(data,curr,foo,id){
      if(data){
        console.log(foo)
        for(let i=0;i<data.length;i++){
          console.log(data[i])
          curr.push({
            label:data[i][foo],
            value:data[i][id],
          })
        }
        console.log(curr)
      }
    },

    selDept(){
      this.$https.post('/mobiles/selPushDept',{
        userId:this.nowUser
      }).then((res)=>{
        //console.log(res.data.data)
        /*this.deptList=res.data;
        for(var i=0;i<this.deptList.length;i++){
          this.deptField.push(this.deptList[i].fullName);
        }*/
        this.formatLacation(res.data,this.deptField,"fullName","deptId")
      }).catch(err => {
        console.log(err);
      });
    },
    selectDept(value){
      this.did=value;
      this.showDept=false;
      this.pid="";
      this.postField=[];
      this.getPosById(value);
    },
    //根据部门id获取相关岗位
    getPosById(id){
      //console.log(id)
      this.$https.post('/mobiles/selPushPos', {
        userId:this.nowUser,
        deptId:id,
      }).then((res)=>{
        /*this.posList=res.data;
        for(var i=0;i<this.posList.length;i++){
          this.postField.push(this.posList[i].name);
        }*/
        this.formatLacation(res.data,this.postField,"name","id")

      }).catch(err => {
        console.log(err);
      })
    },
    //点击选中的职位
    selectPos(value){
      this.pid=value;
      this.showPos=false;
    },
    //指定推送人为招聘组所有的人
    getDesignatedPusher(){
      this.$https.post('/mobiles/selPusher',{
        roleName:"招聘组",
        userId:this.nowUser
      }).then((res)=>{
        /*this.pusherList=res.data.data;
        for(var i=0;i<this.pusherList.length;i++){
          this.pusherField.push(this.pusherList[i].name);
        }*/
        this.designatedPusher=res.data.data[0].userId;
        this.formatLacation(res.data.data,this.pusherField,"name","userId")
      }).catch(err => {
        console.log(err);
      })
    },
    //点击选中的推送人
    selectPusher(value){
      this.designatedPusher=value;
      this.showPusher=false;
    },

    // 返回
    goBack() {
      this.$router.go(-1);
    },
    // 提交
    submitPost() {

      if (!this.isNull()) return false;
      /*var s = '';
      s = this.getImageUrl(s);*/
      // on confirm

      this.$https.post(this.addPush, {
        userId:this.nowUser,
        deptId:this.deptId,
        did:this.did,
        pid:this.pid,
        designatedPusher: this.designatedPusher,
        uname:this.uname,
        usex:this.usex,
        uphone: this.uphone,
        relation: this.relation,
        fileId:this.fileList[this.fileList.length-1].fileId,
        zdGroup:this.zdGroup,
        groupName:this.groupName,
        english:this.english,

      }).then(res => {
        res;
        Toast("推送成功");
        this.isShow=true;
        this.message="已内推";
        this.$router.go(-1);
      }).catch(err => {
        console.log(err)
      });
    },
    //判断相关数据是否为空
    isNull() {
      if (this.designatedPusher == '') {
        this.$toast("指定推送人不能为空");
        return false;
      }
      if (this.did == '') {
        this.$toast("部门不能为空");
        return false;
      }
      if (this.pid == '') {
        this.$toast("岗位不能为空");
        return false;
      }
      if (this.english == '') {
        this.$toast("英语等级不能为空");
        return false;
      }
      if (this.fileList == '') {
        this.$toast("附件不能为空");
        return false;
      }
      return true;
    },

    /*beforeRead(file){
      if (file.type!=='application/pdf'){
        Toast('请上传pdf格式文件');
        return false;
      }
      return true;
    },*/
    //监听上传变动
    afterRead() {
      let formData = new FormData();
      //fs=$("#file")[0].files;
      //console.log(this.$refs.inputer);
      var fs=this.$refs.inputer.files;
      for (let i = 0; i < fs.length; i++) {
        //let d = fs[0]
        formData.append("fileList", fs[i]);  //文件上传处理
        /*if(/.(PDF|pdf)$/.test(d.name)){ //文件必须为文档
          formData.append("fileList", fs[i]);  //文件上传处理
        }else{
          alert('上传文件必须是PDF！')
          return false
        }*/

      }
      /*if (this.fileList.length > 0) {
          for (let index = 0; index < this.fileList.length; index++) {
              const element = this.fileList[index];
              //element.file取的是File文件对象
              formData.append("fileList", element.file);
          }
      }*/
      //console.log(file);
      this.$https.post(this.uploadImageUrl,formData,{
        timeout: 0, //无超时时间
        headers: { "Content-Type": "multipart/form-data" }
      }).then(res => {
        debugger
        this.fileList=res.data.data;
        this.fileName=this.fileList[this.fileList.length-1].fileName;
        this.fileUrl=this.fileList[this.fileList.length-1].filePath;

        /*this.fileUrl=pdf.createLoadingTask(this.fileUrl)
        this.fileUrl.then(pdf=>{
            this.numPages=pdf.numPages
        });
        //alert(this.fileUrl);
        this.pdfSrc = `data:application/pdf;base64,${this.fileList[this.fileList.length-1].filePath}`;*/
        Toast("上传完成");
      }).catch(err => {
        console.log(err);
      })
    },

  },
};
</script>

<style lang="scss" scoped>
.main {
  display: flex;
  justify-items: center;
  margin: 0 auto;
  .el-form {
    padding-right: 10px;
    .el-form-item {
      .el-form-item__label {
        line-height: 16px;
      }
      margin-bottom: 5px;
    }
  }
}
</style>